package com.chen.blue.service.business.impl;

import com.chen.blue.converter.business.ContestPersonInfoConverter;
import com.chen.blue.dictionary.AuditDict;
import com.chen.blue.domain.model.business.ContestApplyActiveModel;
import com.chen.blue.domain.model.business.ContestPersonInfoModel;
import com.chen.blue.domain.model.system.DeptModel;
import com.chen.blue.domain.vo.business.ContestPersonInfoVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.mapper.business.ContestPersonInfoMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.business.IContestApplyActiveService;
import com.chen.blue.service.business.IContestPersonInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.blue.service.system.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 竞赛人员信息表 服务实现类
 * </p>
 *
 * @author CLT
 * @since 2024-05-26
 */
@Service("iContestPersonInfoService")
public class IContestPersonInfoServiceImpl extends ServiceImpl<ContestPersonInfoMapper, ContestPersonInfoModel> implements IContestPersonInfoService {

    @Resource
    private IDeptService iDeptService;

    @Resource
    private IContestApplyActiveService iContestApplyActiveService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(Long personId) throws DtoNullException {

        if (Objects.isNull(personId)) throw new DtoNullException("参数为空");

        // 判断是否可以执行删除操作（只有在人员信息填报修改阶段可以操作）
        validateUpdateOrAddEnabled(personId);

        return super.removeById(personId);

    }

    @Override
    public DataResult<List<ContestPersonInfoVo>> queryByApplyId(Long applyId) throws DtoNullException {

        if (Objects.isNull(applyId)) {
            throw new DtoNullException("参数为空");
        }

        Map<String, Object> wrap = new HashMap<>();
        wrap.put("apply_id", applyId);
        List<ContestPersonInfoModel> contestPersonInfoModelList = super.listByMap(wrap);
        List<ContestPersonInfoVo> contestPersonInfoVos = ContestPersonInfoConverter.modelToVoList(contestPersonInfoModelList);

        List<DeptModel> deptModels = iDeptService.list();
        List<ContestPersonInfoVo> data = Objects.nonNull(contestPersonInfoVos) && !contestPersonInfoVos.isEmpty() ? contestPersonInfoVos.stream().peek(var1 -> {
            for (DeptModel deptModel : deptModels) {
                if (deptModel.getId().equals(var1.getDeptId()))
                    var1.setDeptName(deptModel.getName());
            }
        }).collect(Collectors.toList()) : null;

        DataResult<List<ContestPersonInfoVo>> dataResult = new DataResult<>();
        dataResult.setData(data);

        return dataResult;
    }

    /**
     * 验证是否可操作
     *
     * @param personId
     */
    protected void validateUpdateOrAddEnabled(Long personId) throws DtoNullException {

        Long activeId = super.getById(personId).getActiveId();

        if (Objects.nonNull(activeId)) {

            ContestApplyActiveModel temp = iContestApplyActiveService.getById(activeId);

            if (Objects.nonNull(temp)) {
                if (!(AuditDict.STAGE_STAGE_SECOND.equals(temp.getStageCurrent()) &&
                        AuditDict.ROLE_INNER_NO.equals(temp.getPersonAudit()) &&
                        AuditDict.AUDIT_STATUS_NO.equals(temp.getAuditStatus()) &&
                        AuditDict.CURRENT_STATUS_NOT_REPORT.equals(temp.getStageCurrentStatus()))) {

                    throw new DtoNullException("当前流程状态不可操作");

                }
            }

        }

    }

}
